package com.zjm.zdk;

import java.util.Stack;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;

public class ThreadPoolExecutorUseCase {

    private static final int COUNT_BITS = Integer.SIZE - 3; // 29
    private static final int CAPACITY   = (1 << COUNT_BITS) - 1;

    // runState is stored in the high-order bits
    private static final int RUNNING    = -1 << COUNT_BITS; // 11100000000000000000000000000000
    private static final int SHUTDOWN   =  0 << COUNT_BITS; // 0000000000000000000000000000000
    private static final int STOP       =  1 << COUNT_BITS; // 0100000000000000000000000000000
    private static final int TIDYING    =  2 << COUNT_BITS; // 1000000000000000000000000000000
    private static final int TERMINATED =  3 << COUNT_BITS; // 1100000000000000000000000000000

    private static int runStateOf(int c)     { return c & ~CAPACITY; }
    private static int workerCountOf(int c)  { return c & CAPACITY; }
    private static int ctlOf(int rs, int wc) { return rs | wc; }
    private static final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0)); // 最终结果还是  RUNNING

    public static void main1(String[] args) {
        System.out.println(SHUTDOWN > STOP);
        System.out.println(ctl.get());
    }


    public static void main2(String[] args) throws InterruptedException {
        int corePoolSize = 2;
        int maximumPoolSize = 10;
        long keepAliveTime = 1;
        TimeUnit unit = TimeUnit.SECONDS;
        BlockingQueue workQueue = new LinkedBlockingQueue();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);

        threadPoolExecutor.submit(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        threadPoolExecutor.execute(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        System.out.println(threadPoolExecutor.getPoolSize());
        System.out.println(threadPoolExecutor.getCorePoolSize());
        System.out.println(threadPoolExecutor.getLargestPoolSize());
        System.out.println(threadPoolExecutor.getMaximumPoolSize());


        Thread.sleep(1000 * 1000);
    }

    public static void main(String[] args) throws InterruptedException {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                System.out.println("park");
                LockSupport.park();
            }
        };
        Thread thread = new Thread(r);
        thread.start();
        Thread.sleep(500);
        System.out.println("unpark");
        LockSupport.unpark(thread);
        Object obj = new Object();
        System.out.println(obj);
        Stack stack = null;
    }
}
